<?php
namespace App\Repositories;

use App\Models\User;
use App\Traits\BaseFun;

/**
 * 权限仓库
 */
class UserRepository
{

    //新增
    public function create($param)
    {
        $param['password']=md5($param['password']);
        $sys = new User();
        $sys->fill($param)->save();
        return $sys;
    }
    public function infoBytoken($token)
    {
        return User::
        // select('id', 'nickname', 'token_time')->
        where('token', $token)->first();
    }
    //权限详情
    public function info($id)
    {
        return User::where('id', $id)->first();
    }
    //更新
    public function update($param)
    {
        $sys =  User::find($param['id']);
        if(isset($param['pass'])){
            $param['pass'] = md5($param['pass']);
        }
        return $sys->fill($param)->save();
    }
    //删除
    public function delete($id)
    {
        return User::where('id', $id)->delete();
    }
    //列表
    public function list($param=[]){
        $res=User::with('company1')->with('company2')->orderBy('created_at','desc');
        if (isset($param['nickname'])) {
            $res=$res->where('nickname','like',"%{$param['nickname']}%");
        }
        if (isset($param['account'])) {
            $res=$res->where('account','like',"%{$param['account']}%");
        }
        if (isset($param['company1'])) {
            $res=$res->where('company1',$param['company1']);
        }
        if (isset($param['company2'])) {
            $res=$res->where('company2',$param['company2']);
        }
        if (isset($param['identity'])) {//1为教师2为学生
            $res=$res->where('identity',$param['identity']);
        }
        if (isset($param['state'])) {//状态1待审核2审核通过3拒绝通过
            $res=$res->where('state',$param['state']);
        }
        return  $res->paginate(10);
    }
    public function indexlist($param=[]){
        $res=User::orderBy('created_at','desc');
        return  $res->get();
    }
}
